package com.niit.bigdata.consumer;

import com.niit.bigdata.service.RedisService;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
public class KafkaConsumer {
    private RedisService redisService;

    KafkaConsumer(RedisService redisService) {
        this.redisService = redisService;
    }

    @KafkaListener(topics = "result", groupId = "result")
    public void listen(@Header("kafka_receivedMessageKey") String key, @Payload String message) {
        if (key == null) {
            return;
        }
        redisService.setWithExpiration(key, message, 60 * 60 * 24 * 7);
    }
    @KafkaListener(topics = "orders", groupId = "orders")
    public void listenOrders(@Header("kafka_receivedMessageKey") String key, @Payload String message) {
        redisService.set("order", message);
        System.out.println(message);
    }
}
